﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Game
{
    public class State
    {
        public enum Transition
        {
            Current, Next
        }

        public State()
        {
        }

        public virtual void Activate()
        {
            //Not impl
        }

        public virtual void Deactivate()
        {
            //Not impl
        }

        public virtual Transition Update(Events.ProcessUpdate evnt)
        {
            return Transition.Current;
        }
    }

    /*-------------------------------------------------------------------------
    | StateManager:
    | ------------
    | StateManager
    |---------------------------------------------------------------------------*/
    public class StateManager : Dictionary<State, State>
    {
        protected State mCurrentState = null;

        public StateManager()
        {
        }

        ~StateManager()
        {
            Clear();
        }

        new public void Clear() 
        {
            mCurrentState = null;
            base.Clear();
        }

        public void SetState(State state)
        {
            if(mCurrentState != null)
            {
                Log.Hint(String.Format("State {0} is deactivate...begin", mCurrentState.ToString()));
                mCurrentState.Deactivate();
                Log.Hint(String.Format("State {0} is deactivate...end", mCurrentState.ToString()));
            }

            if ((mCurrentState = state) != null)
            {
                Log.Hint(String.Format("State {0} is activate...begin", mCurrentState.ToString()));
                mCurrentState.Activate();
                Log.Hint(String.Format("State {0} is activate...end", mCurrentState.ToString()));
            }
        }

        public State Current
        {
            get { return mCurrentState; }
        }

        public bool Update(Events.ProcessUpdate evnt)
        {
            if (mCurrentState != null)
            {
                switch (mCurrentState.Update(evnt))
                {
                    case State.Transition.Current :
                        break;

                    case State.Transition.Next:
                        SetState(this[mCurrentState]);
                        break;
                }
                return true;
            }
            return false;
        }
    }
}
